﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
namespace Indie_Game_Contest
{
    class EnemyAnimation
    { 
        Rectangle source; 
        private Texture2D texture;
        private int rows;
        private int columns;
        private int current_frame;
        private int total_frames;
        private int width;
        private bool done;

        public bool Done
        {
            get { return done; }
            set { done = value; }
        }

        private int count = 0;
        public int Width
        {
            get { return width; }
            set { width = value; }
        }
        private int height;

        public int Height
        {
            get { return height; }
            set { height = value; }
        }
        public int Total_frames
        {
            get { return total_frames; }
            set { total_frames = value; }
        }

        public int Columns
        {
            get { return columns; }
            set { columns = value; }
        }
        public int Rows
        {
            get { return rows; }
            set { rows = value; }
        }
        public Texture2D Texture
        {
            get { return texture; }
            set { texture = value; }
        }
        public EnemyAnimation()
         {

         }
        public void LoadSpiteData(Texture2D texture, int rows, int columns)
        {
            Texture = texture;
            Rows = rows;
            Columns = columns;
            Total_frames = Rows * Columns;
            current_frame = 0;
            
        }
        public void LoadSpiteData(Texture2D texture, int rows, int columns ,int total)
        {
            Texture = texture;
            Rows = rows;
            Columns = columns;
            Total_frames = total;
            current_frame = 0;

        }
        public void LoadSpiteData(Texture2D texture, int rows, int columns, int total, int width, int height)
        {
            Texture = texture;
            Rows = rows;
            Columns = columns;
            Total_frames = total;
            current_frame = 0;
            Width = width;
            Height = height;

        }
        public void Update()
        {
            current_frame++;
            if (current_frame == Total_frames)
            {
                Done = true;
                current_frame = 0;
            }

        }
        public void Update( int step)
        { 
            if ( count % step == 0)
                current_frame++;
            if (current_frame == Total_frames)
                current_frame = 0;
            count++;

        }
        public void Draw(SpriteBatch sprite, Vector2 position)
        {
            int width = Width / Columns;
            int height = Height / Rows;
            int row = (int)((float)current_frame / (float)Columns);
            int column = current_frame % Columns;

            
            source = Cell(row * column - 1);
            Rectangle dest = new Rectangle((int)position.X, (int)position.Y, width, height);
            
            sprite.Draw(Texture, dest, source, Color.White);
           


        }
        public Rectangle Cell(int id)
        {
            

            if (id >= 0 && id <= rows * columns - 1)
            {
                Rectangle rect = new Rectangle(
                (id % Columns) * Width,
                (id / Columns) * Width,
                ((id % Columns) * Width) + Width,
                ((id / Columns) * Width) + Height);
                return rect;
            }
            return new Rectangle();

        }
        public void Draw(SpriteBatch sprite, Vector2 position, Vector2 spriteStart,float angle,float scale,bool flip)
        {
            int width = Texture.Width/ Columns ;
            int height =Texture.Height/ Rows    ;
            int row = (int)((float)current_frame / (float)Columns) + (int)spriteStart.X;
            int column = (current_frame % Columns )+  (int)spriteStart.Y;

           Rectangle source = new Rectangle(width * column, height * row, width, height);
           Rectangle dest = new Rectangle((int)position.X, (int)position.Y, width, height);

           if(flip)
               sprite.Draw(Texture, position, source, Color.White, angle, new Vector2(Width/2,Height/2),scale,SpriteEffects.FlipHorizontally,0);
           else
               sprite.Draw(Texture, position, source, Color.White, angle, new Vector2(Width/2, Height/2), scale, SpriteEffects.None, 0);
          // WorldData.spriteBatch.Draw(TextureManager.Instance.GetTexture(GameplayState.Instance.EnemyID), position, null, Color.White, angle, new Vector2(16,16), scale, SpriteEffects.FlipHorizontally, 0);
        }
    }
    
}
